﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MinerManager
{
	public partial class Utility
	{
		/// <summary>
		/// 软件名称
		/// </summary>
		public const string SoftName = "矿工管家";

		/// <summary>
		/// 版本号
		/// </summary>
		public const string Version = "2.0.0.0";
		
		#region string AutoLoginPath 自动登录文件路径。

		/// <summary>
		/// 自动登录文件路径。
		/// </summary>
		public static string AutoLoginPath
		{
			get
			{
				string result = System.IO.Path.Combine(Environment.CurrentDirectory, "AutoLogin.txt");
				return result;
			}
		}

		#endregion

		#region string MinerStatusLogPath 矿机状态日志问文件路径。

		/// <summary>
		/// 矿机状态日志问文件路径。
		/// </summary>
		public static string MinerStatusLogPath
		{
			get
			{
				string result = System.IO.Path.Combine(Environment.CurrentDirectory, "MinerStatusLog.txt");
				return result;
			}
		}

		#endregion

		#region string GetStatusName(int status) 获取状态名称。

		/// <summary>
		/// 获取状态名称。
		/// </summary>
		/// <param name="status">状态码</param>
		/// <returns></returns>
		public static string GetStatusName(int status)
		{
			string result = string.Empty;

			switch (status)
			{
				case 0: result = "不在线"; break;
				case 1: result = "在线"; break;
				case 2: result = "挖矿中"; break;
			}

			return result;
		}

		#endregion
		
		#region string Get(string url, Encoding enCoding, string userAgent = "") Get方式获取数据。

		/// <summary>
		/// Get方式获取数据。
		/// </summary>
		/// <param name="url">连接地址</param>
		/// <param name="enCoding">字符编码</param>
		/// <param name="userAgent">User-agentHTTP 标头的值</param>
		/// <returns></returns>
		public static string Get(string url, Encoding enCoding, string userAgent = "")
		{
			string result = string.Empty;

			try
			{
				global::System.Net.HttpWebRequest request = (global::System.Net.HttpWebRequest)global::System.Net.WebRequest.Create(url);
				request.UserAgent = userAgent;
				request.Credentials = global::System.Net.CredentialCache.DefaultCredentials;
				global::System.Net.WebResponse response = request.GetResponse();
				using (global::System.IO.Stream readStream = response.GetResponseStream())
				{
					global::System.IO.StreamReader reader = new global::System.IO.StreamReader(readStream, enCoding);
					result = reader.ReadToEnd();
					reader.Close();
					reader.Dispose();
				}
				response.Close();
			}
			catch { }

			return result;
		}

		#endregion

		#region string Post(...) Post方式提交并获取返回数据。

		/// <summary>
		/// Post方式提交并获取返回数据。
		/// </summary>
		/// <param name="url">链接地址</param>
		/// <param name="queryString">参数</param>
		/// <param name="enCoding">字符编码</param>
		/// <param name="userAgent">User-agentHTTP 标头的值</param>
		/// <returns></returns>
		public static string Post(string url, string queryString, Encoding enCoding, string userAgent = "")
		{
			string result = string.Empty;

			byte[] bytes = enCoding.GetBytes(queryString);
			global::System.Net.HttpWebRequest request = (global::System.Net.HttpWebRequest)global::System.Net.WebRequest.Create(url);
			request.Method = "POST";
			//request.ContentType = "application/x-www-form-urlencoded";
			request.ContentType = "application/json";
			request.Accept = "application/json;charset=UTF-8";
			request.UserAgent = userAgent;
			request.ContentLength = bytes.Length;

			global::System.IO.Stream outStream = request.GetRequestStream();
			outStream.Write(bytes, 0, bytes.Length);
			outStream.Close();

			//接收HTTP做出的响应
			global::System.Net.WebResponse response = request.GetResponse();
			using (global::System.IO.Stream readStream = response.GetResponseStream())
			{
				global::System.IO.StreamReader reader = new global::System.IO.StreamReader(readStream, enCoding);

				Char[] read = new Char[256];
				int count = reader.Read(read, 0, 256);
				while (count > 0)
				{
					result += new global::System.String(read, 0, count);
					count = reader.Read(read, 0, 256);
				}

				reader.Close();
				reader.Dispose();
			}
			response.Close();

			return result;
		}

		/// <summary>
		/// Post方式提交并获取返回数据（UTF-8）
		/// </summary>
		/// <param name="url">链接地址</param>
		/// <param name="queryString">参数</param>
		/// <param name="userAgent">User-agentHTTP 标头的值</param>
		/// <returns></returns>
		private static string Post(string url, string queryString, string userAgent = "")
		{
			return Post(url, queryString, Encoding.UTF8, userAgent);
		}

		#endregion

		#region T Clone<T>(T obj) 利用序列化完成引用对象的深度复制。

		/// <summary>
		/// 利用序列化完成引用对象的深度复制。
		/// </summary>
		/// <typeparam name="T">引用对象类型</typeparam>
		/// <param name="obj">要复制的引用对象</param>
		/// <returns></returns>
		public static T Clone<T>(T obj)
		{
			using (System.IO.Stream objectStream = new System.IO.MemoryStream())
			{
				//利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
				System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
				formatter.Serialize(objectStream, obj);
				objectStream.Seek(0, System.IO.SeekOrigin.Begin);
				return (T)formatter.Deserialize(objectStream);
			}
		}

		#endregion
	}
}
